//
//  String+Extension+SHA1.swift
//  
//
//  Created by HOA on 2020/12/29.
//

import Foundation
import UIKit
import CommonCrypto

extension String {
    
    public func sha1() -> String {
        
        if let data = self.data(using: .utf8) {
            
            var digest = [UInt8](repeating: 0, count: Int(CC_SHA1_DIGEST_LENGTH))
            
            let newData = NSData(data: data)
            CC_SHA1(newData.bytes, CC_LONG(data.count), &digest)
            let output = NSMutableString(capacity: Int(CC_SHA1_DIGEST_LENGTH))
            for byte in digest {
                output.appendFormat("%02x", byte)
            }
            return output as String
            
        }
        else {
            return ""
        }
    }
    
}
